﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using SQLExecute;

public partial class list_schools : System.Web.UI.Page
{
    school s = new school();
//  string strSchool_ID;
    static string strSchoolID;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["username"] != null && Session["fullname"] != null)
            {
                Init_Combo();
                BindGrid();
                lUsername.Text = Session["username"].ToString();
            }
            else Response.Redirect("login/login.aspx");
        }
    }

    private void Init_Combo()
    { 
        cbCity.Items.Insert(0,new ListItem("Ho Chi Minh","HCM"));
        cbCity.Items.Insert(1,new ListItem("Ha Noi","HN"));
        cbCity.SelectedIndex = 0;

    }
    private void BindGrid()
    { 
      

        DataSet ds = s.Get_List_School();
        gdSchools.DataSource = ds;
        gdSchools.DataBind();
        gdSchools.Columns[4].Visible = false;
        gdSchools.Columns[5].Visible = false;

    }
    private void EmptyTextbox()
    {
        txtSchoolID.Text = "";
        txtSchoolName.Text = "";
        txtTele.Text = "";
        txtAddress.Text = "";

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (txtSchoolID.Text != "")
        {
            s.SchoolID = txtSchoolID.Text.Trim();
            s.SchoolName = txtSchoolName.Text.Trim();

            if (!s.School_Exists())
            {
                if (txtAddress.Text != "")
                    s.Address = txtAddress.Text.Trim();
                if (txtTele.Text != "")
                    s.Tele = txtTele.Text.Trim();
                s.City = cbCity.SelectedValue;
                s.Insert_School();
                EmptyTextbox();
                BindGrid();
            }
            else omb.ShowMessage("Tên trường học đã có.");
        }
        else omb.ShowMessage("Vui long nhap ma truong.");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("index.aspx");
    }
    protected void gdSchool_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdSchools.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (txtSchoolID2.Text!="" && txtSchoolName2.Text!="") 
            s.Update_School( strSchoolID, txtSchoolID2.Text, txtSchoolName2.Text);
       // string sql="update tbl_schools set school_id ='"+txtSchoolID2.Text +"',schoolname'"+txtSchoolName2.Text+"' 
        BindGrid();
     }
    protected void School_Edit(object sender, EventArgs e)
    {

        LinkButton lnkButton = sender as LinkButton;
        GridViewRow row = (GridViewRow)lnkButton.NamingContainer;
        strSchoolID = gdSchools.DataKeys[row.RowIndex].Value.ToString();
       
        SqlDataReader r;
        string sql="select * from tbl_schools where school_id=N'"+ strSchoolID +"'";

        r = SqlHelper.ExecuteReader(Common.strConnect, CommandType.Text, sql);
        if (r.HasRows)
        while (r.Read())
        {
            txtSchoolID2.Text= r["school_id"].ToString();
            txtSchoolName2.Text = r["schoolname"].ToString();
        }
       
        popupEdit.Show();
    }
    protected void gdSchools_RowEditing(object sender, GridViewEditEventArgs e)
    {
       
       // int idx = gdSchools.SelectedIndex;
        //strSchool_ID = gdSchools.DataKeys[idx].Value.ToString();
        //string 
        //string str = gdSchools.Rows[idx].Cells[3].Text;
       

       
    }
    protected void gdSchools_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("lnkButtonDelete");
            l.Attributes.Add("onclick", "javascript:return "
                            + "confirm('Are you sure you want to delete this record " + DataBinder.Eval(e.Row.DataItem, "schoolname") + "')");
        }
    
    }
    protected void gdSchools_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdSchools.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    private void Del_School(string strID)
    { 
        string sql="delete tbl_schools where school_id=N'"+strSchoolID+"'";  
        
        SqlHelper.ExecuteNonQuery(Common.strConnect,CommandType.Text,sql);


  
    }
    protected void gdSchools_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Del_School(strSchoolID);
    }
    protected void gdSchools_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            strSchoolID = e.CommandArgument.ToString();
            Del_School(strSchoolID);
            BindGrid();
        }
    }
    protected void lnkLogout_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Response.Redirect("../login/login.aspx");
    }
}
